﻿using Coldairarrow.Entity.Goods;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Goods
{
    public class BTB_BannerBusiness : BaseBusiness<BTB_Banner>, IBTB_BannerBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        public BTB_BannerBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口
        /// <summary>
        /// 限时特惠轮播图分页列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageResult<BTB_Banner>> GetPreferentialListAsync(PageInput<BTB_Banner> input)
        {
            var _banner = input.Search;
            var result = GetIQueryable().Where(a => a.Type == 2);
            var where = LinqHelper.True<BTB_Banner>();
            int count = result.Count();
            //品牌
            if (!string.IsNullOrEmpty(_banner.Name))
            {
                var aa = Db.GetIQueryable<BTB_Goods>(true).Where(z => z.GoodsName.Contains(_banner.Name));
                result = result.Where(k => k.Goodsid == aa.FirstOrDefault().ID);
            }
            var q = await result.Select(s => new BTB_Banner
            {
                id = s.id,
                Banner_Url = s.Banner_Url,
                Banner_Link = s.Banner_Link,
                CreatorId = s.CreatorId,
                CreateTime = s.CreateTime,
                Deleted = true,
                Goodsid = Db.GetIQueryable<BTB_Goods>(true).Where(a => a.ID == s.Goodsid).FirstOrDefault().GoodsName,
                Name = Db.GetIQueryable<BTB_Goods>(true).Where(a => a.ID == s.Goodsid).FirstOrDefault().GoodsName,
                EditTime = s.EditTime,
                Editor = s.Editor,
                Type = s.Type,
                Sort = s.Sort

            }).Skip((input.PageIndex - 1) * input.PageRows)
                .Take(input.PageRows)
                .ToListAsync();

            return new PageResult<BTB_Banner> { Data = q, Total = count };
        }
        /// <summary>
        /// 小程序轮播图分页列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageResult<BTB_Banner>> GetHomeListAsync(PageInput<ConditionDTO> input)
        {
            var result = GetIQueryable().Where(a => a.Type == 1);
            var where = LinqHelper.True<BTB_Banner>();
            int count = result.Count();
            var q = await result.Select(s => new BTB_Banner
            {
                id = s.id,
                Banner_Url = s.Banner_Url,
                Banner_Link = s.Banner_Link,
                CreatorId = s.CreatorId,
                CreateTime = s.CreateTime,
                Deleted = true,
                Goodsid = Db.GetIQueryable<BTB_Goods>(true).Where(a => a.ID == s.Goodsid).FirstOrDefault().GoodsName,
                EditTime = s.EditTime,
                Editor = s.Editor,
                Type = s.Type,
                Sort = s.Sort

            }).Skip((input.PageIndex - 1) * input.PageRows)
                .Take(input.PageRows)
                .ToListAsync();

            return new PageResult<BTB_Banner> { Data = q, Total = count };
        }
        /// <summary>
        /// 查看指定轮播图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List< BTB_Banner>> GetTheDataAsync(string id)
        {
            return await GetIQueryable().Where(q => q.id == id).Select(s => new BTB_Banner
            {
                id = s.id,
                Banner_Url = s.Banner_Url,
                Banner_Link = s.Banner_Link,
                CreatorId = s.CreatorId,
                CreateTime = s.CreateTime,
                Deleted = s.Deleted,
                Goodsid = s.Goodsid,
                Editor = s.Editor,
                EditTime = s.EditTime,
                Type = s.Type,
                Sort = s.Sort,
                Name = Db.GetIQueryable<BTB_Goods>(true).Where(z=>z.ID == s.Goodsid).FirstOrDefault().GoodsName
            }).ToListAsync();
        }
        /// <summary>
        /// 添加轮播图
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>

        public async Task AddDataAsync(BTB_Banner data)
        {
            BTB_Banner data1 = new BTB_Banner()
            {
                id = GuidHelper.GenerateKey(),
                Banner_Url = data.Banner_Url,
                Banner_Link = data.Banner_Link,
                CreatorId = _operator.UserId,
                CreateTime = data.CreateTime,
                Deleted = false,
                Goodsid = data.Goodsid,
                Editor = data.Editor,
                EditTime = data.EditTime,
                Type = data.Type,
                Sort = data.Sort
            };
            await InsertAsync(data1);
        }

        public async Task UpdateDataAsync(BTB_Banner data)
        {
            BTB_Banner data1 = new BTB_Banner()
            {
                id = data.id,
                Banner_Url = data.Banner_Url,
                Banner_Link = data.Banner_Link,
                CreatorId = data.CreatorId,
                CreateTime = data.CreateTime,
                Editor = _operator.UserId,
                EditTime = data.EditTime,
                Deleted = false,
                Goodsid = data.Goodsid,
                Type = data.Type,
                Sort = data.Sort
            };
            await UpdateAsync(data1);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion
        #region PC
        /// <summary>
        /// 查看指定轮播图
        /// </summary>
        /// <returns></returns>
        public async Task<List<BTB_Banner>> GetData(int type)
        {
            return await GetIQueryable().Where(q => q.Type == type).OrderBy("Sort", "DESC").ToListAsync();
        }
        #endregion
        #region 私有成员

        #endregion
    }
}